<template>
  <!--  页面容器 -->
  <el-container class="layout-container">
    <!-- 头部 -->
    <el-header class="header">
      <div class="logo">
        <img src="@/assets/img/heima.png"
             alt="">
        <h3>电商后台管理系统</h3>
      </div>
      <div class="right">
        <el-dropdown>
          <span class="el-dropdown-link">
            {{username}}，你好！<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>全屏</el-dropdown-item>
            <el-dropdown-item @click.native="quit">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <!-- 底部 -->
    <el-container class="container">
      <!-- 左边 -->
      <el-aside :width=" isCollapse ? '64px':'230px' "
                class="menu">

        <!-- 切换图标 -->
        <div class="line">
          <i class="el-icon-s-unfold"
             v-if="isCollapse"
             @click="isCollapse=!isCollapse"></i>
          <i class="el-icon-s-fold"
             v-else
             @click="isCollapse=!isCollapse"></i>
        </div>
        <!-- 菜单内容 -->
        <!-- router 属性 表示开启路由模式， menu-item 写上index就是跳转地址 -->
        <el-menu router
                 :default-active="$route.path"
                 :collapse-transition="false"
                 background-color="#333744"
                 text-color="#fff"
                 :collapse="isCollapse">
          <el-submenu :index="item.path"
                      v-for="(item,index) in menus"
                      :key="index">
            <template slot="title">
              <i :class="['iconfont',fontClassObj['icon-'+item.id]]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+val.path"
                          v-for="(val,idx) in item.children"
                          :key="idx">{{val.authName}}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边 -->
      <el-main class="main">
        <!-- 路由出口（二级） -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'layout',
  data () {
    // 从本地存储读取登录后的信息
    const user = JSON.parse(localStorage.getItem('HEIMA_SHOP_USER'))
    return {
      username: user.username,
      isCollapse:false, // 不折叠
      menus:[], // 菜单数据
      fontClassObj:{
        'icon-125':'icon-user',
        'icon-103':'icon-tijikongjian',
        'icon-101':'icon-shangpin',
        'icon-102':'icon-danju',
        'icon-145':'icon-baobiao'
      }
    }
  },
  created () {
    this.getMenu()
  },
  methods: {
    // 左侧菜单
    async getMenu () {
      // 1.发请求
      const { data: res } = await this.$axios.get('/menus')
      // 2.错误处理
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 3.成功处理
      this.menus = res.data
    },
    // 退出功能
    quit () {
      localStorage.removeItem('HEIMA_SHOP_USER')
      this.$router.push('/login')
    }
  }
}
</script>
<style scoped lang="less">
.layout-container{
  width:100vw;
  height: 100vh;
  .header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    .logo{
      display: flex;
      align-items: center;
      img{
        height: 50px;
      }
      h3{
        font-size: 20px;
        font-weight: normal;
        margin-left: 10px;
      }
    }
    .el-dropdown-link{
      color: #fff;
    }
  }
  .menu{
    background-color: #333744;
    .line{
      text-align: center;
      color: #fff;
      line-height: 30px;
      background-color: #6a6d79;
      cursor: pointer;
    }
    .iconfont{
      color:#fff;
      margin-right: 5px;
    }
    .el-menu{
      border:none;
    }
  }
  .main{
    background-color: #eee;
    overflow-y: scroll;
    height: calc(100vh - 60px);
  }
}
</style>
